package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	public class nod extends MovieClip
	{
		var numarNodText:TextField=new TextField();
		
		public var nWidth:int=40;
		public var nHeight:int=30;
		public var numarNod:int=0;
		public var utilizat:Boolean=false;
		
		var muchii : Array = new Array();
		
		public function nod(nodCurent:int)
		{
			numarNod=nodCurent;			
			addEventListener(Event.ADDED_TO_STAGE, hAddedToStage);	
		}
		
		private function hAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, hAddedToStage);
			initializare(numarNod);			
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, trage);
			
			this.doubleClickEnabled = true;
		}
		
		private function trage(e:MouseEvent):void
		{
			this.addEventListener(MouseEvent.MOUSE_UP,lasa);
			
			this.addEventListener(MouseEvent.MOUSE_MOVE,updateMuchii);
			
			parent.swapChildrenAt(parent.getChildIndex(this),parent.numChildren-1);
			
			e.target.startDrag();
		}
		
		private function lasa(e:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_UP,lasa);
			
			this.removeEventListener(MouseEvent.MOUSE_MOVE,updateMuchii);
			
			e.target.stopDrag();
		}
		
		private function updateMuchii(e:MouseEvent):void
		{
			var i:int;
			for (i = 0; i < muchii.length; i++) 
				(muchii[i] as muchie).deseneaza();
			
			e.updateAfterEvent();
		}
		
		public function addMuchie(mc : muchie) : Boolean
		{
			if(existaMuchie(mc))
			{
				return false;	
			}
			else
				muchii.push(mc);
			return true;
		}
		
		public function existaMuchie(mc : muchie):Boolean
		{
			var exista:Boolean=false;
			var i:int;
			
			for(i=0;i<muchii.length;i++)
			{
				var extern1:int;
				var extern2:int;
				
				extern1=(muchii[i] as muchie).nodStanga.numarNod;
				if(extern1==this.numarNod)
					extern1=(muchii[i] as muchie).nodDreapta.numarNod;
				
				extern2=mc.nodStanga.numarNod;
				if(extern2==this.numarNod)
					extern2=mc.nodDreapta.numarNod;
				
				if(extern1==extern2)
					exista=true;
			}
			return exista;
		}
		
		public function refaNod():void
		{
			utilizat=false;
			deseneaza();
		}
		
		public function vecini():Array
		{
			var rez : Array =new Array();
			var i:int;
			var nd:nod;
			
			for(i=0;i<muchii.length;i++)
			{
				if(this==(muchii[i] as muchie).nodStanga)
					nd=(muchii[i] as muchie).nodDreapta;
				else
					nd=(muchii[i] as muchie).nodStanga;
				
				rez.push(nd);
			}
			return rez;
		}
		
		public function deseneaza():void
		{
			this.graphics.clear();
			
			this.graphics.beginFill(0x00adbb,0.9);
			this.graphics.drawEllipse(0,0,nWidth,nHeight);
		}
		
		public function deseneazaDf():void
		{
			this.graphics.clear();
			
			this.graphics.beginFill(0x00efed,0.9)
			this.graphics.drawEllipse(0,0,nWidth,nHeight);
		}
		
		public function deseneazaApasat():void
		{
			this.graphics.clear();
			
			this.graphics.beginFill(0x00bdef,0.9);
			this.graphics.drawEllipse(0,0,nWidth,nHeight);
		}
		
		public function stergeVecin(nd:nod):void
		{
			var i:int;
			var nd1:nod;
			
			for(i=0;i<muchii.length;i++)
			{
				if(this==(muchii[i] as muchie).nodStanga)
					nd1=(muchii[i] as muchie).nodDreapta;
				else
					nd1=(muchii[i] as muchie).nodStanga;
				
				if(nd1==nd)
				{
					muchii.splice(i,1);
					return ;
				}
			}
		}
		
		private function initializare(nodCurent:int):void
		{				
			this.x = Math.random() * (stage.stageWidth - nWidth);
			this.y = 30+ Math.random() * (stage.stageHeight - 2*nHeight);
			
			deseneaza();
			
			if (nodCurent != 0)
			{
				numarNodText.text=""+nodCurent;
				
				numarNodText.x=nWidth/4;
				numarNodText.y=nHeight/4;
				
				numarNodText.width=20;
				numarNodText.height=20;
				
				numarNodText.mouseEnabled=false;
				
				this.addChild(numarNodText);
			}
		}
	}
}